package entity.immobile;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;

import loading.ImageLoader;
import main.GameList;
import entity.Entity;

public class Wall extends Entity implements Solid
{
	private int height, width;
	public Wall(Point2D pos, GameList game, ImageLoader img, int height, int width) 
	{
		super(pos, game, img);
		if (height < 1)
			height = 1;
		if (width < 1)
			width = 1;
		this.height = height;
		this.width = width;
		setCircular(false);
	}
	public int getWidth() 
	{
		return width*super.getWidth();
	}
	public void setWidth(int width) 
	{
		this.width = width;
	}
	public int getHeight() 
	{
		return height*super.getHeight();
	}
	public void setHeight(int height) 
	{
		this.height = height;
	}
	@Override
	public void draw(Graphics2D g)
	{
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				getImage().draw(g, AffineTransform.getTranslateInstance(getPosition().getX()+i*getImage().width(), getPosition().getY()+j*getImage().height()));
			}
		}
	}
	/**
	 * 
	 * @param ent The entity that might collide with the wall
	 * @return The side that the entity is colliding with, 0 = top, 1 = right, 2 = bottom, 3 = left
	 */
	public int getCollisionSide(Entity ent)
	{
		//get all the points
		Point2D.Double point1 = (Double)getPosition();
		Point2D.Double point2 = new Point2D.Double(getPosition().getX()+width*getImage().width(), getPosition().getY());
		Point2D.Double point3 = new Point2D.Double(point2.getX(), point2.getY()+height*getImage().height());
		Point2D.Double point4 = new Point2D.Double(point1.getX(), point3.getY());
		//get all the lines
		Line2D.Double top = new Line2D.Double(point1, point2);
		Line2D.Double right = new Line2D.Double(point2, point3);
		Line2D.Double bottom = new Line2D.Double(point3, point4);
		Line2D.Double left = new Line2D.Double(point4, point1);
		//smallest distance so far
		double smllDist = Math.abs(top.ptSegDist(ent.getPosition()));
		double dist = Math.abs(right.ptSegDist(ent.getPosition()));
		//the wall the smallest distance represents
		int wall = 0;
		//check all walls to see if their distances are smaller
		if (dist < smllDist)
		{
			smllDist = dist;
			wall = 1;
		}
		dist = Math.abs(bottom.ptSegDist(ent.getPosition()));
		if (dist < smllDist)
		{
			smllDist = dist;
			wall = 2;
		}
		dist = Math.abs(left.ptSegDist(ent.getPosition()));
		if (dist < smllDist)
		{
			smllDist = dist;
			wall = 3;
		}
		//if the smallest distance is smaller than the entity's radius, return the wall
		if (smllDist < ent.getRadius())
			return wall;
		else
			 return -1;
	}
}
